package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.dapan.DapanDimDictDefine;
import com.hllxd.hism.star.basic.service.model.doris.HealthScoreAppidEntity;
import com.hllxd.hism.star.basic.service.service.DimDefineManagerService;
import com.hllxd.hism.star.basic.service.service.HealthScoreService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author lixiyong01
 * @date 2023.04.02
 * 健康分查询
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/healthScore")
public class HealthScoreController2 {

    @Autowired
    private HealthScoreService healthScoreService;

    @Autowired
    private DimDefineManagerService defineManagerService;

    @Data
    public static class RequestDefine {
        public Long id;
        public String dt;
        public String dt2;
        public String tag;

        public String group;
        public String ownerId;
        public String orgCnName;
        public String startDt;
        public String endDt;
        public String existHealthScore;
        public String tableName;

        public String appId;
    }

    @RestGrpcMapping("/listHealthScoreAppid.do")
    public NTSTATUS listHealthScoreAppid(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            RequestDefine aa = params.driverObject(RequestDefine.class);
            List<HealthScoreAppidEntity> items = healthScoreService.listHealthScoreAppid(aa.dt, aa.dt2, aa.tag, aa.ownerId, aa.appId, aa.orgCnName);
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


//    @RestGrpcMapping("/listAllTableScore.do")
//    public NTSTATUS listAllTableScore(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            RequestDefine aa = params.driverObject(RequestDefine.class);
//            List<TblStorageHealthScore> items = healthScoreService.listAllTableScore(aa.startDt, aa.endDt, aa.ownerId, aa.orgCnName, aa.existHealthScore);
//            log.info("items: {}.", items.size());
//            result.setData(items);
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

//    @RestGrpcMapping("/listGroupHealthScore.do")
//    public NTSTATUS listGroupHealthScore(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            RequestDefine aa = params.driverObject(RequestDefine.class);
//            List<TblStorageHealthScore> items = healthScoreService.listGroupHealthScore(aa.group, aa.startDt, aa.endDt, aa.ownerId, aa.orgCnName);
//            log.info("items: {}.", items.size());
//            result.setData(items);
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

//    @RestGrpcMapping("/getStorageHealthScoreDetail.do")
//    public NTSTATUS getStorageHealthScoreDetail(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            RequestDefine aa = params.driverObject(RequestDefine.class);
//            TblStorageHealthScore item = healthScoreService.getStorageHealthScoreDetail(aa.getTableName(), aa.getDt());
//            result.setData(item);
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }


    @RestGrpcMapping("/setHealthScore.do")
    public NTSTATUS setHealthScore(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            DapanDimDictDefine aa = params.driverObject(DapanDimDictDefine.class);
            Object res;
            if ("add".equals(aa.getOpt())) {
                res = defineManagerService.setDimDefine(aa);
            } else {
                res = defineManagerService.delDimDefineByCondition(aa.getTypeCode(), aa.getItemKey());
            }
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
}
